home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13254 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Opening STDOUT As BINARY
  5. Date: Fri, 05 Apr 96 15:13:54 GMT
  6. Organization: none
  7. Message-ID: <828717234snz@genesis.demon.co.uk>
  8. References: <3161CAD8.296C@netrover.com> <4k1hoe$hn0@sun001.spd.dsccc.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4k1hoe$hn0@sun001.spd.dsccc.com>
  15.            jmccarty@sun1307.spd.dsccc.com "Mike McCarty" writes:
  16.  
  17. >In article <3161CAD8.296C@netrover.com>,
  18. >Stephane Charette  <charrick@netrover.com> wrote:
  19. >)I've come across a problem, and I believe that it is
  20. >)caused by limitations in C/C++.  Can anyone help on the
  21. >)following:
  22. >)
  23. >)I need to output binary information to the console, which
  24. >)is then redirected to a file by the o/s.  At the moment,
  25. >)I'm using printf( "%c", mychar ) to output the the binary
  26. >)data one character at a time as it comes in.
  27.  
  28. You could have simply written putchar(mychar)
  29.  
  30. >)However, it seems that the character #7 (0x07, bell) gets
  31. >)sent to stdout followed by an automatic CR (0x0A).  Every
  32. >)other character in the ASCII table works fine!  Is there
  33. >)a way to reopen stdout in binary mode instead of as a text
  34. >)stream? The extra 0x0A characters are corrupting the data!
  35.  
  36. You could use something like:
  37.  
  38.     freopen(filename, "wb", stdout)
  39.  
  40. but you need a filename that corresponds to a suitable device. That
  41. is implementation specific, if it exists at all. For DOS that might be "CON".
  42. The disadvantage to this is that it will cancel any redirection of stdout
  43. that might have been performed when your program was run.
  44.  
  45. >)I've also tried "cout" and "putc", but they yield the same
  46. >)result.
  47. >)
  48. >)Why do I need this:  this application runs as a CGI-BIN,
  49. >)and the web server EXPECTS the output to be sent to
  50. >)stdout.  I cannot fwrite() this information to a physical
  51. >)file since the web server is capturing the console output!
  52.  
  53. It might work in this case.
  54.  
  55. >The behavior you describe has -nothing- to do with C.
  56.  
  57. It is the C language that defines that stdin, stdout and stderr are
  58. opened in text rather than binary mode.
  59.  
  60. >It has to do with
  61. >the peculiarities of the operating system you are using. Is htis DOS? If
  62. >so, then investigate putting stdout in binary mode. Your compiler should
  63. >have a way to do this. Look into variables such as _fmode, __mode etc.
  64.  
  65. That is probably a good way to go in this case.
  66.  
  67. -- 
  68. -----------------------------------------
  69. Lawrence Kirby | fred@genesis.demon.co.uk
  70. Wilts, England | 70734.126@compuserve.com
  71. -----------------------------------------
  72.